package com.hcj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcj.mapper.CartMapper;
import com.hcj.pojo.Cart;
import com.hcj.service.CartService;
import com.hcj.util.MyResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper mapper;
    Page<Cart> cartPage = new Page<>();
    @Override
    public boolean add(Cart cart) {
        int insert = mapper.insert(cart);
        if (insert==1){
            return true;
        }
        else return false;
    }
    @Override
    public Cart selectById(Cart cart){


        return mapper.selectById(cart.getId());
    }
    @Override
    public Cart selectByOne(Cart cart){
        //同一个用户，同一个商品，只能有一个订单
        return mapper.selectOne(new LambdaQueryWrapper<Cart>()
                .eq(Cart::getUid,cart.getUid())
                .eq(Cart::getPid,cart.getPid()));
    }
    @Override
    public boolean delete(Cart cart) {
        LambdaQueryWrapper<Cart> eq = new LambdaQueryWrapper<Cart>().eq(Cart::getId, cart.getId());
        int delete = mapper.delete(eq);
        if(delete==1){
            return true;
        }
        return false;
    }

    @Override
    public Page<Cart> selectAllPage(Integer currentPage, Integer pageSize) {
//        Page<Cart> cartPage = new Page<>();
        cartPage.setSize(pageSize);
        cartPage.setCurrent(currentPage);
        Page<Cart> cartPage1 = mapper.selectPage(cartPage, null);
        System.out.println("cartPage1 = " + cartPage1.getRecords());
        return mapper.selectPage(cartPage,null);

    }

    @Override
    public Page<Cart> selectAllPgeByUid(Integer uid, Integer currentPage, Integer pageSize) {
        //设置分页大小和当前页面
        Page<Cart> cartPage = new Page<>();
        cartPage.setSize(pageSize);
        cartPage.setCurrent(currentPage);
        LambdaQueryWrapper<Cart> eq = new LambdaQueryWrapper<Cart>().eq(Cart::getUid, uid);
        return mapper.selectPage(cartPage, eq);

    }

    @Override
    public Page<Cart> selectAllPgeByPid(Integer pid, Integer currentPage, Integer pageSize) {
        //设置分页大小和当前页面

        cartPage.setSize(pageSize);
        cartPage.setCurrent(currentPage);
        LambdaQueryWrapper<Cart> eq = new LambdaQueryWrapper<Cart>().eq(Cart::getPid, pid);
        return mapper.selectPage(cartPage, eq);

    }

    @Override
    public MyResponse updateAll(Cart cart) {
        //1.判断订单是否存在
        Cart cart1 = mapper.selectOne(new LambdaQueryWrapper<Cart>().eq(Cart::getId, cart.getId()));
        if (cart1==null){
            return new MyResponse("100","订单不存在");
        }
        int i = mapper.updateById(cart);
        if (i==1){
            return new MyResponse("100","订单添加成功");
        }
        return new MyResponse("200","订单修改异常");


    }

    @Override
    public Page<Cart> selectLikeName(String name,Integer pageSize,Integer currentPage) {
        //设置分页大小和当前页面
//        Page<Cart> cartPage = new Page<>();
        cartPage.setSize(pageSize);
        return null;
    }
    @Override
    public List<Cart> selectByUid(Integer uid){
        return mapper.selectList(new LambdaQueryWrapper<Cart>().eq(Cart::getUid, uid));

    }
}
